import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

# 设置中文字体
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

# 读取Excel文件
try:
    data = pd.read_excel('d:\\实训\\minda-practical-training\\project\\练习一\\FhjlViewDD.xlsx')
except FileNotFoundError:
    print("错误：未找到文件FhjlViewDD.xlsx，请检查文件路径")
    exit()

# 将日期列转换为datetime格式
data['创建时间'] = pd.to_datetime(data['创建时间'])

# 筛选6月份数据
june_data = data[data['创建时间'].dt.month == 6]

# 按发货地分组求和并排序
location_sum = june_data.groupby('发货地')['净重'].sum().sort_values(ascending=False)

# 绘制饼状图
plt.figure(figsize=(12, 8))
plt.pie(location_sum.values, labels=location_sum.index, autopct='%1.1f%%', 
        startangle=90, wedgeprops={'width':0.4, 'edgecolor':'w'})
plt.title('6月份各发货地矿粉发货量占比', fontsize=16, pad=20)
plt.axis('equal')  # 保证饼图是圆形

# 保存图表
plt.savefig('d:\\实训\\minda-practical-training\\project\\练习一\\shipping_location_pie_chart.png', 
            bbox_inches='tight', dpi=300, facecolor='white')
plt.show()

print("统计结果：")
print(location_sum)